package com.red.star.macalline.sched.service;

import com.red.star.macalline.data.entity.SmsSetting;
import com.red.star.macalline.data.mappers.SmsSettingMapper;
import com.red.star.macalline.data.utils.CheckUtil;
import com.red.star.macalline.data.utils.SmsUtil;
import com.red.star.macalline.sched.entity.ActiveChat;
import com.red.star.macalline.sched.entity.SmsLog;
import com.red.star.macalline.sched.mapper.SmsLogMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author nofish.yan@gmail.com
 * @date 2018/3/21.
 * 短信服务类
 */
@Service
public class SmsService {

    private Logger logger = LoggerFactory.getLogger(SmsService.class);

    @Value("${sched.sms.cIntervalTime}")
    private Integer cIntervalTime = 15;

    @Value("${sched.sms.bIntervalTime}")
    private Integer bIntervalTime = 15;

    @Resource
    private ClientService clientService;

    @Resource
    private SmsLogMapper smsLogMapper;

    @Resource
    private FansService fansService;

    @Resource
    private SmsSettingMapper smsSettingMapper;

    /**
     * 发送短信唤醒聊天用户
     */
    public void sendMsgToAwakenChat() {
        // 获取需要发送短信唤醒的用户信息
        List<ActiveChat> activeChats = clientService.listFansWithChatSessionInTime();
        // 无用户满足短信唤醒条件
        if (CheckUtil.isEmpty(activeChats)) {
            logger.debug("无用户满足短信唤醒条件");
            return;
        }
        // 遍历用户，发送短信
        for (ActiveChat activeChat : activeChats) {
            // 已经发送过该会话的唤醒短信，则不再发送
            if (isSendForSession(activeChat.getFansId(), activeChat.getChatSessionId())) {
                logger.debug("今天已经发送过该会话的短信给用户了");
            } else {
                // 发送短信
                String smsContent = getSmsContent(activeChat);
                // 短信内容为空
                if (!CheckUtil.isEmpty(smsContent)) {
                    SmsUtil.sendSms(activeChat.getMobile(), getSmsContent(activeChat));
                    // 保存短信发送日志
                    SmsLog smsLog = new SmsLog(activeChat.getFansId(), new Date(), activeChat.getChatSessionId(), activeChat.getMobile());
                    smsLogMapper.insert(smsLog);
                }
            }
        }
    }

    /**
     * 是否发送过该会话的唤醒短信给某个用户
     *
     * @param fansId        用户ID
     * @param chatSessionId 会话ID
     * @return
     */
    private boolean isSendForSession(Integer fansId, String chatSessionId) {

        boolean result = smsLogMapper.countSmsLogByFansAndSession(fansId, chatSessionId, cIntervalTime, bIntervalTime) > 0;


        return result;
    }

    /**
     * 获取短信文本
     *
     * @param activeChat 要发送短信的对象
     * @return
     */
    private String getSmsContent(ActiveChat activeChat) {
        String result = "";
        if (fansService.isEmployee(activeChat.getFansId())) {
            // 导购员
            SmsSetting smsSetting = smsSettingMapper.selectByPrimaryKey(1);
            if (smsSetting != null) {
                result = smsSetting.getBContent();
            }
        } else {
            // C端用户
            SmsSetting smsSetting = smsSettingMapper.selectByPrimaryKey(1);
            if (smsSetting != null) {
                result = smsSetting.getCContent();
            }
        }
        return result;
    }
}
